package com.c.ctgproject.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.service.ServiceCommonOrderEntity
import com.c.ctgproject.ui.service.ChangePayWayActivity
import com.c.ctgproject.ui.service.ServiceOrderDetailActivity
import com.c.ctgproject.ui.service.ServiceOrderEditActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.handlerNull
import org.jetbrains.anko.startActivity

class ServiceOrderAdapter(private val context: Context, private val mList: List<ServiceCommonOrderEntity.DataBean.ListBean>, private var needEdit: Boolean = false,private  var dataType:Int=0,private var statue:String="") : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_service_order_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mOrderIdTV.text=data.orderCode.handlerNull()
            //订单状态
            holder.mOrderStatueTV.text = when (data.status) {
                "0" -> "已取消"
                "1" -> "待处理"
                "2" -> "待物流处理"
                "3" -> "已完成"
                "4" -> "待沟通"
                "5" -> "回退订单"
                "6" -> "待自送"
                else -> ""
            }
            "车主姓名:  ${data.carOwner.handlerNull()}".also { holder.mOwnerNameTV.text = it }
            "联系方式:  ${data.ownerPhone.handlerNull()}".also { holder.mPhoneTV.text = it }
            "车  牌  号:  ${data.ownerPlate.handlerNull()}".also { holder.mCarNumberTV.text = it }
            "创建时间:  ${data.createTime.handlerNull()}".also { holder.mTimeTV.text = it }
            holder.mCarFromTV.text = when (data.orderSource) {
                "1" -> "订单来源:  微信"
                "2" -> "订单来源:  400电话"
                "3" -> "订单来源:  车主自送"
                "4" -> "订单来源:  业务员收车"
                else -> "订单来源:  "
            }

            holder.mChangeOrderBTN.visibility = if (needEdit) View.VISIBLE else View.GONE
            //详情
            holder.mParentLL.setOnClickListener {
                context.startActivity<ServiceOrderDetailActivity>(MyConstants.DATA to data)
            }
            //修改 预订单和  意向订单
            holder.mChangeOrderBTN.setOnClickListener {
                context.startActivity<ServiceOrderEditActivity>(
                    MyConstants.DATA to data,
                    MyConstants.DATA_TYPE to dataType
                )
            }
            //取消订单处理
            if(statue=="3"){
                holder.mChangePayWayBTN.visibility=View.VISIBLE
            }else{
                holder.mChangePayWayBTN.visibility=View.GONE
            }
            holder.mChangePayWayBTN.setOnClickListener {
                context.startActivity<ChangePayWayActivity>(MyConstants.SUBORDER_ID to data.suborderId.handlerNull())
            }

        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mOrderNumber: TextView = view.findViewById(R.id.mOrderNumber)
        val mOrderStatueTV: TextView = view.findViewById(R.id.mOrderStatueTV)
        val mOwnerNameTV: TextView = view.findViewById(R.id.mOwnerNameTV)
        val mPhoneTV: TextView = view.findViewById(R.id.mPhoneTV)
        val mCarNumberTV: TextView = view.findViewById(R.id.mCarNumberTV)
        val mOrderIdTV: TextView = view.findViewById(R.id.mOrderIdTV)
        val mCarFromTV: TextView = view.findViewById(R.id.mCarFromTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mChangeOrderBTN: Button = view.findViewById(R.id.mChangeOrderBTN)
        val mChangePayWayBTN: Button = view.findViewById(R.id.mChangePayWayBTN)
    }
}